// ---------------------------------------------------------------------
//
// -- 声音信息
// -- author £
//
// ---------------------------------------------------------------------

const soundDataKey = 'HexagonDFTTSoundKey';

let SoundInfo = {
    soundData: null,
    
    /** 启动 */
    startup: function () {
        //设置声音数据
        this.soundData = cc.sys.localStorage.getItem(soundDataKey);
        
        if (!this.soundData) {
            this.soundData = {soundVol: 1, seVol: 1, soundMute: false, seMute: false, lang: 0};
        } else {
            this.soundData = JSON.parse(this.soundData);
            
            if (this.soundData.soundVol === undefined)
                this.soundData.soundVol = 1;
            if (this.soundData.seVol === undefined)
                this.soundData.seVol = 1;
            if (this.soundData.soundMute === undefined)
                this.soundData.soundMute = false;
            if (this.soundData.seMute === undefined)
                this.soundData.seMute = false;
            if (this.soundData.lang === undefined)
                this.soundData.lang = 0;
        }

        this.saveSoundData();
        
    },

    /** 是否无效数据 */
    invalid: function () {
        return this.soundData === null;
        // return true;
    },

    /** 保存声音数据 */
    saveSoundData: function () {
        cc.sys.localStorage.setItem(soundDataKey, JSON.stringify(this.soundData));
    },

    setSoundVol: function (value) {
        this.soundData.soundVol = value;
        this.saveSoundData();
    },

    getSoundVol: function (origin) {
        if (origin)
            return this.soundData.soundVol;

        return this.getSoundMute() ? 0 : this.soundData.soundVol;
    },

    setSEVol: function (value) {
        this.soundData.seVol = value;
        this.saveSoundData();
    },

    getSEVol: function (origin) {
        if (origin)
            return this.soundData.seVol;

        return this.getSEMute() ? 0 : this.soundData.seVol;
    },

    setSoundMute: function (value) {
        this.soundData.soundMute = value;
        this.saveSoundData();
    },

    getSoundMute: function () {
        return this.soundData.soundMute;
    },

    setSEMute: function (value) {
        this.soundData.seMute = value;
        this.saveSoundData();
    },

    getSEMute: function () {
        return this.soundData.seMute;
    },

    setSoundLang: function (value) {
        this.soundData.lang = value;
        this.saveSoundData();
    },

    getSoundLang: function () {
        return this.soundData.lang;
    },

    getSoundPrefix: function (pos) {
        return (this.getSoundLang() === 0 ? 'sh_' : 'pt_') +
            ((require('CommonInfo').getSexByPos(pos) === 1) ? 'm_' : 'f_');
    },
};

module.exports = SoundInfo;